<?php

namespace app\admin;

use app\BaseController;
use think\Request;
use think\exception\ValidateException;
use think\exception\HttpResponseException;
use app\admin\common;
use think\facade\View;
/**
 * @title project 基类
 *
 */
class ControllerBase
{
    public $ERROR="";
    public $CODE=201;
    public $SUCCESS;
    public $REQUEST;
    public static $s_server;
  	public function checkParam(request $request){
  		$_param=$this->REQUEST->param();
  		$_decryption=$_param;
  		//1.获取查询员工数据
  		if(isset($_param['data']) && !empty($_param['data'])){
  			$_data=json_decode($_param['data'],true);
  			$_decryption['data']=$_data;
  		}
  		
  		$this->checkSign($_param['sign'],$_decryption);
  		//如果发现有json数据 那么就进行处理 否则跳过
  		if(isset($_data) && !empty($_data)){
  			$this->REQUEST->setRoute(['user_id'=>'','merch_id'=>'']);
  			$_data['request_data']['merch_id']=$_param['merch_id'];
  			$_data['request_data']['user_id']=$_param['user_id'];
  			 $this->REQUEST->setRoute($_data);
  		}else{
  			return true;
  		}
  	}
  	
  	public function checkSign($_sign,$_decryption){
  		$_key_obj=new Key();
  		unset($_decryption['sign']);
  	//	var_dump(isset($_decryption['sign_no_check']) && $_decryption['sign_no_check']==Request()->url(),$_key_obj->sign($_decryption),$_sign);exit;
  		//$_generate_sign=$_key_obj->sign($_decryption);
  		//免验签方法 同一个方法验签过后 再次调用该方法不进行验签
  	//	var_dump($_decryption);exit;
  		if(isset($_decryption['sign_no_check']) && $_decryption['sign_no_check']==Request()->url())
  		{
  			return true;
  		}
  		if($_key_obj->sign($_decryption)!=$_sign)
  		{
  			$_return_data=[
  			'data'=>false,
  			'msg'=>'验签失败',
  			'code'=>201
  			];
  			echo json_encode($_return_data);exit;
  		}
  		$this->REQUEST->setRoute(['sign_no_check'=>Request()->url()]);
  		return true;
  	}

  	
    public function checkVailDate($_class = '', $_type = '')
    {
        $_param = $this->REQUEST->param();
        try {
            $r = validate($_class)->scene($_type)->check($_param);
            
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.field.error', 1);
            $this->ERROR = $this->ERROR . ',' . $e->getError();
            $this->CODE=201;
            return false;
        }
        return true;
    }

    //存日志
    public function requestLog($function = '', $param = [], $name = '')
    {
        $common = new common();
        return $common->requestLog($function, $param, $name);
    }

    //记录所有CODE和msg
    public function getMessageCode($field, $flag)
    {
        if ($flag == 1) {
            $this->ERROR = config($field . '.msg');
        }
        $this->CODE = config($field . '.code');
        $this->SUCCESS = config($field . '.msg');
    }

    //公用请求方法一些不需要调用第三方的直接返回
    public function commonFunction($function)
    {
        try {
            $data = self::$s_server->$function();
            if ($data == false) {
                if(!isset(self::$s_server->CODE)){
                    self::$s_server->CODE=201;
                }
                return common::return_json_layui($data, self::$s_server->ERROR, self::$s_server->CODE);
            } else {
                if(!isset(self::$s_server->CODE)){
                    self::$s_server->CODE=0;
                }
                return common::return_json_layui($data, self::$s_server->SUCCESS, self::$s_server->CODE);
            }
        } catch (HttpResponseException $e) {
            //程序报错的情况下会出现这种
            exit;
        }
    }




    function getList(){
        return $this->commonFunction(__function__);
    }
    function getDetail(){
        return $this->commonFunction(__function__);
    }
    function add(){
        return $this->commonFunction(__function__);
    }
    function edit(){
        return $this->commonFunction(__function__);
    }
    function del($id = 0){
        return $this->commonFunction(__function__);
    }


}

?>